package com.xtaifghy.wugui;

import com.xtaifghy.wugui.util.Util;

import java.util.ArrayList;
import java.util.Random;

public class GameState22 implements GameStateInterface{
    private boolean [] state = {true,true,true,true,true,true,true,true};
    private boolean [] goalState = {false,true,false,false,false,false,false,false};
    Random rand = new Random();
    private static final int[][] actions={
            {0,1,3},//1
            {0,1,2},//2
            {1,2,4},//3
            {0,5,3},//4
            {1,4,6,3},//5
            {2,4,7},//6
            {6,5,3},//7
            {6,5,7},//8
            {6,4,7},//9
    };

    @Override
    public void doAction(int a){
        if(a<0||a>actions.length)return;
        for (int i:actions[a]) {
            state[i]=!state[i];
        }
    }

    @Override
    public boolean testState(){
        boolean flag = true;
        for (int i = 0; i < state.length; i++) {
            if(state[i]!=goalState[i]) flag = false;
        }
        return flag;
    }

    @Override
    public ArrayList<boolean[]> getPaths(){
        ArrayList<boolean[]> paths = new ArrayList<>();
        expandNode(new boolean[9],0,paths);
        return paths;
    }

    public GameState22() {
        randomGoalState();
    }

    private void expandNode(boolean[] action, int depth,ArrayList<boolean[]> paths){
        //if(paths.size()>0) return;//找到1个解后，结束查找。
        if(depth==9){
            if(Util.testActionSeq(state,goalState,action)){
                paths.add(action.clone());
            }
            return;
        }else if(depth<9){
            action[depth]=false;
            expandNode(action,depth+1,paths);
            action[depth]=true;
            expandNode(action,depth+1,paths);
        }
    }

    @Override
    public void setState(boolean[] state) {
        this.state = state;
    }

    @Override
    public boolean[] getState() {
        return state.clone();
    }

    @Override
    public void setGoalState(boolean[] goalState) {
        this.goalState = goalState;
    }

    @Override
    public boolean[] getGoalState() {
        return goalState.clone();
    }

    @Override
    public void randomGoalState(){
        /*for (int i = 0; i < goalState.length; i++) {
            goalState[i] = rand.nextBoolean();
        }*/
        goalState = Util.intToBArr(rand.nextInt(256),8);
    }
}
